<template>
  <business-layout title='运维人员'>
    <div class='table-operator' slot='query'>
      <a-form :form='form' layout='inline' @submit='handleQueryData'>
        <a-form-item label=''>
          <a-input placeholder='搜索值班人姓名或者电话' v-decorator='["name"]' />
        </a-form-item>
        <a-form-item>
          <a-button type='primary' html-type='submit'>
            <a-icon type='search' />
            查询
          </a-button>

          <a-button type='reset' @click='handleRest()'>
            重置
          </a-button>
        </a-form-item>
      </a-form>
    </div>

    <template slot='btns'>
      <a-button type='primary' icon='plus' @click='handleAdd'>新建</a-button>
      <a-button type='primary' style='margin-right: 10px' @click='importVisable = true'>
        <a-icon type='cloud-upload' />
        导入
      </a-button>
      <a-button type='primary' style='margin-right: 10px' @click='exportData'>
        <a-icon type='download' />
        全部导出
      </a-button>
      <a-dropdown v-if='selectedRowKeys.length > 0' style='margin-right: 10px'>
        <a-menu slot='overlay'>
          <a-menu-item key='1' @click='handleDeletes'>
            <a-icon type='delete' />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style='margin-left: 8px'>
          批量操作
          <a-icon type='down' />
        </a-button>
      </a-dropdown>
    </template>

    <s-table
      ref='table'
      size='default'
      rowKey='id'
      :columns='columns'
      :data='loadData'
      showPagination='auto'
    >
      <!--   :rowSelection='rowSelection'    :alert='true'-->
      <span slot='serial' slot-scope='text, record, index'>
        {{ index + 1 }}
      </span>
      <span slot='description' slot-scope='text'>
        <ellipsis :length='4' tooltip>{{ text }}</ellipsis>
      </span>

      <span slot='action' slot-scope='text, record'>
        <template>
          <a @click='handleEdit(record)'>编辑</a>
          <a-divider type='vertical' />
          <a class='del-link' @click='handleSub(record)'>删除</a>
        </template>
      </span>
    </s-table>

    <create-form
      ref='createModal'
      :visible='visible'
      :loading='confirmLoading'
      :model='mdl'
      @cancel='handleCancel'
      @ok='handleOk'
    />
    <import-dialog
      :importFileFun='importFun'
      template-path='money/维护人员管理.xlsx'
      :visible='importVisable'
      @close='importVisable = false'
      @refsh='handleRefsh'
    />
  </business-layout>
</template>

<script>
import moment from 'moment'
import { STable, Ellipsis } from '@/components'

import CreateForm from './modules/createDevOps.vue'
import {
  createoperation, deleteoperation,
  downloadoperation,
  findBypageoperation,
  importExceloperation,
  updateoperation
} from '@/api/devOps'
import ImportDialog from '@/components/ImportDialog/index.vue'
import { downLoadEquipment, importEquipment } from '@/api/equipmentManagement'
import { deleteDevices } from '@/api/description'

const columns = [
  {
    title: '序号',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '姓名',
    dataIndex: 'name',
    scopedSlots: { customRender: 'name' }
  },
  {
    title: '电话',
    dataIndex: 'phone'
    // needTotal: true,
    // customRender: (text) => text + ' 次'
  },
  {
    title: '职位',
    dataIndex: 'title'
  },
  {
    title: '值班日期',
    dataIndex: 'ondutyDate'
  },
  {
    title: '值班安排',
    dataIndex: 'arrangement'
  },
  {
    title: '操作',
    dataIndex: 'updatedAt',
    scopedSlots: { customRender: 'action' }
  }
]

export default {
  name: 'TableList',
  components: {
    ImportDialog,
    STable,
    Ellipsis,
    CreateForm
  },
  data() {
    this.columns = columns
    return {
      activeKey: '1',
      // create model
      visible: false,
      confirmLoading: false,
      form: this.$form.createForm(this),
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数

      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        const requestParameters = Object.assign({}, parameter, this.queryParam)
        console.log('loadData request parameters:', requestParameters)
        return findBypageoperation({ ...requestParameters }).then(res => {
          return res
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      importVisable: false
    }
  },
  filters: {},
  created() {
  },
  computed: {
    rowSelection() {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleAdd() {
      this.mdl = null
      this.visible = true
    },
    handleEdit(record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleDeletes() {

    },
    handleRest() {
      this.form.resetFields()
      this.queryParam = {}
      this.$refs.table.refresh()
    },
    handleRefsh() {
      this.$refs.table.refresh()
    },
    exportData() {
      downloadoperation()
    },
    importFun: importExceloperation,
    handleOk() {
      const form = this.$refs.createModal.form
      this.confirmLoading = true
      form.validateFields((errors, values) => {
        if (!errors) {
          if (this.mdl && this.mdl.id > 0) {
            // 修改 e.g.
            let updates = Object.assign(this.mdl, values)
            updateoperation(updates).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('更新成功')
            })
          } else {
            // 新增
            createoperation(values).then(res => {
              this.visible = false
              this.confirmLoading = false
              // 重置表单数据
              form.resetFields()
              // 刷新表格
              this.$refs.table.refresh()

              this.$message.success('新增成功')
            })
          }
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel() {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    handleSub(record) {
      console.log(record,'record')
      const that = this
      this.$confirm({
        title: '提示?',
        content: '当前操作不可逆、是否继续？',
        okType: 'danger',
        async onOk() {
          const result = await deleteoperation(record.id)
          if (result) {
            that.$message.success('删除成功')
            that.$refs.table.refresh()
          }
        },
        onCancel() {
          console.log('Cancel')
        }
      })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    handleQueryData(e) {
      e.preventDefault()
      const form = this.form
      const that = this
      const queryObj = {}
      form.validateFields((error, values) => {
        Object.keys(values).forEach(key => {
          if (values[key]) {
            queryObj[key] = values[key]
          }
        })
        this.queryParam = queryObj
        that.$refs.table.refresh()
      })
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
<style lang='less' scoped>
.table-tab {

}
</style>
